package sleuthkit.util.sleuthkit.processors;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;

import org.apache.log4j.Logger;

import sleuthkit.gui.MainApplication;
import sleuthkit.util.Node;
import sleuthkit.util.StaticHelper;

public class MmlsProcessor extends Processor {

	private String mmls;

	public MmlsProcessor() {
		super();
		StaticHelper.configLogger();
		log.debug("Configured the logger");
		mmls = MainApplication.getInstance().getMmls();
		log.debug("Using the mmls at " + mmls);
	}
	
	/* (non-Javadoc)
	 * @see sleuthkit.util.sleuthkit.processors.Processor#process(java.io.File, javax.swing.tree.MutableTreeNode)
	 */

	@Override
	public boolean process(File file, MutableTreeNode fileDefaultMutableTreeNode) {
		boolean canProcess = false;

		try {
			boolean startProcessingLines = false;
			log.debug("boolean startProcessingLines = " + startProcessingLines);
			Runtime runtime = Runtime.getRuntime();
			log.debug("Runtime runtime = Runtime.getRuntime() resulted in " + runtime);
			String executionStatment = mmls + " -a " + file.getAbsolutePath();
			log.debug("The executionStatment is " + executionStatment);
			Process executionProcess = runtime.exec(executionStatment);
			log.debug("runtime.exec resulted in " + executionProcess);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					executionProcess.getInputStream()));
			String line = null;
			while ((line = in.readLine()) != null) {
				log.debug(line);
				// Slot Start End Length Description
				if (startProcessingLines) {
					// 02: 00:00 0000000063 0009510479 0009510417 NTFS (0x07)
					String partitionNumber = line.split(":")[0];
					fileDefaultMutableTreeNode.insert(
							new DefaultMutableTreeNode(new Node(
									partitionNumber,
									Node.PARTITION_TABLE_ENTRY, line, file)),
							fileDefaultMutableTreeNode.getChildCount());
				}
				if (line.contains("Slot") && line.contains("Start")
						&& line.contains("End") && line.contains("Length")
						&& line.contains("Description")) {
					startProcessingLines = true;
					canProcess = true;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return canProcess;
	}

	/* (non-Javadoc)
	 * @see sleuthkit.util.sleuthkit.processors.Processor#isProcessible(java.io.File)
	 */
	@Override
	public boolean isProcessible(File imageFile) {
		try {
			
			Runtime runtime = Runtime.getRuntime();
			log.debug("Runtime runtime = Runtime.getRuntime() resulted in " + runtime);
			
			String executionStatment = mmls + " -a " + imageFile.getAbsolutePath();
			log.debug("Runtime runtime = Runtime.getRuntime() resulted in " + runtime);
			
			Process executionProcess = runtime.exec(executionStatment);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					executionProcess.getInputStream()));
			String line = null;
			while ((line = in.readLine()) != null) {
				log.debug(line);
				// Slot Start End Length Description
				if (line.contains("Slot") && line.contains("Start")
						&& line.contains("End") && line.contains("Length")
						&& line.contains("Description")) {
					return true;
				}
			}
		} catch (IOException e) {
			log.debug("IOException", e);
		}
		return false;
	}
}
